﻿/*
     作者：熊宸
 *   制作时间：2012年1月9日
 *   简介：将无缩进的xml数据装换为缩进格式输出 。
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Text.RegularExpressions;

namespace XMLFormater
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGO_Click(object sender, EventArgs e)
        {   
            IsolatedStorageFile tmpFile=IsolatedStorageFile.GetUserStoreForDomain();
            IsolatedStorageFileStream tmpStream=tmpFile.CreateFile("tmpFormated");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(rtbOriginalXml.Text);
            doc.Save(tmpStream);
            tmpStream.Close();
            tmpStream = tmpFile.OpenFile("tmpFormated", FileMode.Open);
            StreamReader reader = new StreamReader(tmpStream);
            rtbFormatedXml.Text = reader.ReadToEnd();
            reader.Close();
            tmpStream.Close();
           

        }

        private void CreateTree(XmlDocument doc)
        {
            IsolatedStorageFile tmpFile = IsolatedStorageFile.GetUserStoreForDomain();
            IsolatedStorageFileStream tmpStream = tmpFile.CreateFile("tmpFormatedXml");
            XmlWriter xmlWriter = XmlWriter.Create(tmpStream, new XmlWriterSettings { Indent=true,ConformanceLevel=ConformanceLevel.Auto});
            Stack<XmlElement> stack = new Stack<XmlElement>();
            XmlElement tmpNode;
            
            foreach (XmlElement node in doc.DocumentElement.ChildNodes)
            {
                stack.Push(node);
            }
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement(doc.DocumentElement.LocalName);
            while (true)
            {
                if (stack.Count == 0)//打印结束
                {
                    break;
                }
                tmpNode = stack.Pop();
                xmlWriter.WriteStartElement(tmpNode.LocalName);
                //打印属性
                foreach (XmlAttribute attr in tmpNode.Attributes)
                {
                    xmlWriter.WriteAttributeString(attr.LocalName, attr.InnerText);
                }
                //将所有的子属性推入栈中
                foreach (XmlElement t in tmpNode.ChildNodes)
                {
                    xmlWriter.WriteStartElement(t.LocalName);
                    xmlWriter.WriteString(t.InnerText);
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteValue(tmpNode.Value);
                xmlWriter.WriteEndElement();
                
                //break;
            }
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            tmpStream.Close();
            tmpStream = tmpFile.OpenFile("tmpFormatedXml", FileMode.Open);
            StreamReader reader = new StreamReader(tmpStream);
            string formatedXml = reader.ReadToEnd();
            rtbFormatedXml.Text = formatedXml;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string oldData = rtbFormatedXml.Text.Trim();
            string[] tmpData = oldData.Split('\n');
            string tmp;
            rtbOriginalXml.Text="";
            foreach (string t in tmpData)
            {
                tmp = Regex.Replace(t, "\n", "");
                rtbOriginalXml.Text += tmp.Trim();
            }
        }
    }
}
